package com.salinity.kun.util;

import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
	private static Calendar calendar = null;
	static {
		calendar = Calendar.getInstance();
	}

	/**
	 * 新版的API 月份是从1开始的，Calendar 的月份则是从0开始的，这里要 +1
	 * 
	 * @param date
	 * @return
	 */
	public static LocalDateTime Date2LDTime(Date date) {
		calendar.setTime(date);
		return LocalDateTime.of(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1,
				calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE),
				calendar.get(Calendar.SECOND));
	}

	/**
	 * 新版的API 月份是从1开始的，Calendar 的月份则是从0开始的,这里要 -1
	 * 
	 * @param LDTime
	 * @return
	 */
	public static Date LDTime2Date(LocalDateTime LDTime) {
		calendar.set(LDTime.getYear(), LDTime.getMonthValue() - 1, LDTime.getDayOfMonth(), LDTime.getHour(),
				LDTime.getMinute(), LDTime.getSecond());
		return calendar.getTime();
	}

	public static Calendar Date2Calendar(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar;
	}

	public static Date Calendar2Date(Calendar calendar) {
		return calendar.getTime();
	}
}
